package weaver.interfaces.jjy.duss.util;

import com.sap.mw.jco.JCO;
import org.apache.commons.lang.StringUtils;
import weaver.general.BaseBean;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static weaver.common.DateUtil.getDate;

/**
 * @program: ecology-9-demo
 * @description: sap返回数据公共方法
 * @author: Dusongsong
 * @create: 2019-12-06
 */
public class SAPComMehtod {
    /*
    *@Description: 入职带回基本信息
    *@Param: [out, Table00]
    *@return: java.util.Map
    *@Author: Dusongsong
    *@Date: 2019/12/9
    */
    public static Map SAPreturn(JCO.ParameterList out, JCO.ParameterList Table00) {
        Map<String, String> map = new HashMap<>();
        BaseBean baseBean  = new BaseBean();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //基本信息
        String ygh = "";
        String xm = "";
        String jgbm = "";
        String jllxbm = "";
        String ksrq3 = "";
        String jsrq3 = "";
        String zw = "";
        String gzdw = "";
        String massn = "";
        String flag = "";
        String msg = "";
        FieldValueUtil fieldValue = new FieldValueUtil();
        JCO.Structure stOut = out.getStructure("EV_OUTPUT");
        flag = (String) stOut.getValue("ZFLAG");
        msg = (String) stOut.getValue("ZMESG");
        map.put("flag", flag);
        map.put("msg", msg);

        JCO.Structure st0 = out.getStructure("PS0000");
        massn = (String) st0.getValue("MASSN");
        map.put("massn", massn);

        JCO.Structure st1 = out.getStructure("PS0001");
        ygh = (String) st1.getValue("PERNR");
        map.put("ygh", ygh);

        JCO.Structure st2 = out.getStructure("PS0002");
        xm = (String) st2.getValue("NACHN");
        jgbm = (String) st2.getValue("ZHR_JG");
        //csrq = (String)st2.getValue("GBDAT");
        //csrq = getDate2Str((Date) st2.getValue("GBDAT"));
        map.put("xm", xm);
       // map.put("xb", xb);
        map.put("jgbm", fieldValue.execute("PS0002","ZHR_JG",jgbm));
        //map.put("csrq", csrq);


        JCO.Table t23 = Table00.getTable("PT0023");
        t23.appendRow();
        jllxbm = (String) t23.getValue("ZHR_JLLX");
        gzdw = (String) t23.getValue("ARBGB");
        ksrq3 = (String) t23.getValue("BEGDA");
        jsrq3 = (String) t23.getValue("ENDDA");
        zw = (String) t23.getValue("ZHR_JOBINFO");
        map.put("gzdw", gzdw);
        map.put("jllx",fieldValue.execute("PT0023","ZHR_JLLX",jllxbm));
        map.put("ksrq3", ksrq3);
        map.put("jsrq3", jsrq3);
        map.put("zw", zw);
        baseBean.writeLog(map);
        return map;
    }

    public static Map SAPreturn(String[] st1, JCO.ParameterList out) {
        Map<String, String> map = new HashMap<>();
        map.put("flag", "S");
        String message = "";
        String flag = "S";
        String msg = "";
        for (int i = 0; i < st1.length; i++) {
            JCO.Structure stu = out.getStructure(st1[i]);
            flag = (String) stu.getValue("ZFLAG");
            msg = (String) stu.getValue("ZMESG");
            if ("E".equals(flag)) {
                map.put("flag", "E");
                message += msg + ";";
            }
            System.out.println("-----------------" + st1[i] + "-----------------");
            System.out.println(st1[i] + ".ZFLAG=" + flag);
            System.out.println(st1[i] + ".ZMESG=" + msg);
        }

        if (message.endsWith(";")) {
            message = message.substring(0, message.length() - 1);
        }
        map.put("message", message);

        if ("S".equals(map.get("flag"))) {
            JCO.Structure stu = out.getStructure("EV_OUTPUT");
            map.put("message", (String) stu.getValue("ZMESG"));
        }
        return map;
    }

    public static Map SAPreturnS(String[] tbls, JCO.ParameterList Table00) {
        Map<String, String> map = new HashMap<>();
        map.put("flag", "S");
        String message = "";
        String flag = "S";
        String msg = "";
        for (int i = 0; i < tbls.length; i++) {
            JCO.Table stu = Table00.getTable(tbls[i]);
            flag = (String) stu.getValue("ZFLAG");
            msg = (String) stu.getValue("ZMESG");
            if ("E".equals(flag)) {
                map.put("flag", "E");
                message += msg + ";";
            }
        }

        if (message.endsWith(";")) {
            message = message.substring(0, message.length() - 1);
        }
        map.put("message", message);
        return map;
    }

    /*
    *@Description: 入职信息推送
    *@Param: [stu, out, tbl, tbls]
    *@return: java.util.Map
    *@Author: Dusongsong
    *@Date: 2019/12/9
    */

    public static Map SAPreturn(String[] stu, JCO.ParameterList out, String[] tbl, JCO.ParameterList tbls) {
        Map<String, String> map = new HashMap<>();
        String flag = " ";
        String msg = "";
        String message = "";
        for (int i = 0; i < stu.length; i++) {
            JCO.Structure st1 = out.getStructure(stu[i]);
            flag = (String) st1.getValue("ZFLAG");
            msg = (String) st1.getValue("ZMESG");
            if (flag != null && flag != "") {
                message = "信息提示 ：[" + msg + " ]; ";
                map.put("flag", flag);
                map.put("message", message);
            }else {
                map.put("message","返回的FLAG为空");
            }
        }

        for (int i = 0; i < tbl.length; i++) {
            JCO.Table t = tbls.getTable(tbl[i]);
            flag = (String) t.getValue("ZFLAG");
            msg = (String) t.getValue("ZMESG");
            message = "信息提示 ：[" + msg + " ]; ";
            if (flag != null && flag != "") {
                map.put("flag", flag);
                map.put("message", message);
            }else {
                map.put("message","返回的FLAG为空");
            }


        }
        return map;
    }
    /*
    *@Description: SAP返回信息提示
    *@Param: [st1, out, st2, out2]
    *@return: java.util.Map
    *@Author: Dusongsong
    *@Date: 2019/12/9
    */
    public static Map SAPreturnS(String[] st1, JCO.ParameterList out, String[] st2, JCO.ParameterList out2) {
        BaseBean baseBean = new BaseBean();
        Map<String, String> map = new HashMap<>();
        map.put("flag", "S");
        String message = "";
        String flag = "S";
        String msg = "";
        for (int i = 0; i < st1.length; i++) {
            JCO.Structure stu = out.getStructure(st1[i]);
            flag = (String) stu.getValue("ZFLAG");
            msg = (String) stu.getValue("ZMESG");
            if ("E".equalsIgnoreCase(flag)) {
                map.put("flag", "E");
                message += msg + ";";
                baseBean.writeLog("信息类型="+st1[i]+"报错信息："+message);
            }
        }
        for (int i = 0; i < st2.length; i++) {
            JCO.Table t = out2.getTable(st2[i]);
            for (int j = 0; j < t.getNumRows(); j++) {
                t.setRow(j);
                flag = (String) t.getValue("ZFLAG");
                msg = (String) t.getValue("ZMESG");
                if ("E".equalsIgnoreCase(flag)) {
                    map.put("flag", "E");
                    message += msg + ";";
                    baseBean.writeLog("信息类型="+st2[i]+"报错信息："+message);
                }
            }
        }

        if (message.endsWith(";")) {
            message = message.substring(0, message.length() - 1);
        }
        map.put("message", message);

        if ("S".equals(map.get("flag"))) {
            JCO.Structure stu = out.getStructure("EV_OUTPUT");
            map.put("flag", (String) stu.getValue("ZFLAG"));
            map.put("message", (String) stu.getValue("ZMESG"));
        }
        return map;
    }

    //日期格式转化为字符串
    public static String getDate2Str(Date var0) {
        return getDate(var0, "yyyy-MM-dd");
    }

    //根据身份证号提取性别
    public static String getSex(String idcard) {
        String sex = "";
        if (idcard.length() == 15) {//15位的身份证
            if (Integer.parseInt(idcard.substring(14, 15)) % 2 == 0) {
                sex = "2";
            } else {
                sex = "1";
            }
        } else if (idcard.length() == 18) {//18位身份证
            if (Integer.parseInt(idcard.substring(16,17)) % 2 == 0) {
                sex = "2";//女
            } else {
                sex = "1";//男
            }
        }
        return sex;
    }
    //根据身份证号提取出生日期
    public static String getBirthday(String idcard){
        String birthday="";
        String year="";
        String month="";
        String day="";
        if (StringUtils.isNotBlank(idcard)){
            if (idcard.length() == 15){
                year = "19" + idcard.substring(6, 8);
                month = idcard.substring(8, 10);
                day= idcard.substring(10, 12);
            }else if(idcard.length() ==18){
                year = idcard.substring(6,10);
                month = idcard.substring(10,12);
                day=idcard.substring(12,14);
            }
            birthday = year+"-"+month+"-"+day;
        }
        return birthday;
    }
    //返回SAP数据工具类

}
